/**
 * Created by zxh on 14-7-14.
 */

ms.UIJianCha = ms.UILayer.extend({
    _stepIndex: null,
    ctor: function() {
        this._super();
        this._ccsFileName = "jiancha.json";
        this._stepIndex = 0;
    },

    onEnter: function() {
        this._super();

        for (var i = 0; i < 6; i++) {
            var panle = this["_panelSelect" + i];
            panle.setVisible(false);
            panle.setTouchEnabled(false);
            panle.setBackGroundColorOpacity(0);
        }

        for (var i = 1; i <=6; i++) {
            var img = this["_img" + i];
            img.addTouchEventListener(this.onImageClicked, this);
            img.tag = i;
            img.setTouchEnabled(true);
        }

    },

    onImageClicked: function(sender, type) {
        if (type === ccui.Widget.TOUCH_ENDED) {
            cc.log("onImageClicked");

            if (this._jcdImage.x < 0) {
                var move = cc.MoveBy.create(0.3, cc.p(530, 0));
                this._jcdImage.runAction(move);
            }

            this._jcdImage.loadTexture("res/ccs/jcd" + sender.tag + ".png");
        }

    },

    onContinueButtonTouchEnded: function(sender) {
        if (!sender.isVisible()) {
            return;
        }

        if (this._stepIndex === 0) {
            var move = cc.MoveBy.create(0.3, cc.p(-530, 0));
            this._imageOption.runAction(move);
            this._stepIndex++;
        } else {
            cc.director.runScene(ms.createScene(ms.UIJianCha2));
        }
    },

    onImageOptionTouchEnded: function(sender) {
        var point = sender.getTouchEndPos();
        var point = sender.convertToNodeSpace(point);
        var isShow;
        for (var i = 0; i < 6; i++) {
            var panle = this["_panelSelect" + i];
            var rect = panle.getBoundingBox();
            if (cc.rectContainsPoint(rect, point)) {
                isShow = !panle.isVisible();
                panle.setVisible(isShow);
                break;
            }
        }

        if (!isShow && this._jcdImage.x > 0) {
            var move = cc.MoveBy.create(0.3, cc.p(-530, 0));
            this._jcdImage.runAction(move);
        }
    }
});

ms.UIJianCha2 = ms.UILayer.extend({
   ctor: function() {
       this._super();
       this._ccsFileName = "jiancha2.json";

   },

   onContinueButtonTouchEnded: function() {
       cc.director.runScene(ms.createScene(ms.UIYaoWu));
   }
});

